package com.huika.dealontop.control.homepage.utils.cartproductnumber;

import android.content.Context;
import android.content.Intent;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.huika.dealontop.R;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.homepage.fragment.HomePageFragment;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.UIUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by cailu on 2016/1/13.
 */
public  class  CartProductNumber extends BaseNet {
    private Context mContext;

    public  CartProductNumber(Context context, String id) {
        super(context);
        mContext = context;

        //当为0的时候，不访问网络。
        if("0".endsWith(id)){
            return;
        }
        performToNet(id);
    }

    static final String NUMBER = "com.huika.dealontop.utils.PreferencesHelper.number";

    void performToNet(String id) {
        ///Cart/MyShoppingCart
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();

        ajaxParams.putStringTypeParam("userID", id);
        ajaxParams.putStringTypeParam("type", "2");
        ajaxParams.putStringTypeParam("lang", UIUtils.getString(R.string.common_lang));
        FormResultRequest CartProductRequest = new FormResultRequest(UrlConstants
                .MYSHOPPINGCART, getCartProductNumber, this,
                new TypeToken<RequestResult>() {
                }.getType());
        CartProductRequest.setRequestParams(ajaxParams);
        executeRequest(CartProductRequest);
    }

    ;

    static int mCartProductNumber = 0;
    private Response.Listener<RequestResult> getCartProductNumber =
            new Response.Listener<RequestResult>() {

                @Override
                public void onResponse(RequestResult result) {
                    if (result != null && result.flag == 1) {
                        try {
                            JSONObject json = new JSONObject(result.getRs() + "");
                            String num = json.getString("Count");
                            mCartProductNumber = (int) Float.parseFloat(num);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        Intent intent = new Intent();
                        intent.putExtra(HomePageFragment.KEY_CARTPRODUCTNUMBER, mCartProductNumber);
                        intent.setAction(HomePageFragment.MESSAGE_RECEIVED_CARTPRODUCTNUMBER);
                        mContext.getApplicationContext().sendBroadcast(intent);

                    }
                }
            };

    public void clearCartNumber() {
        mCartProductNumber = 0;
        Intent intent = new Intent();
        intent.putExtra(HomePageFragment.KEY_CARTPRODUCTNUMBER, mCartProductNumber);
        intent.setAction(HomePageFragment.MESSAGE_RECEIVED_CARTPRODUCTNUMBER);
        mContext.getApplicationContext().sendBroadcast(intent);
        return;
    }

    public static int getProductNumber() {
        return mCartProductNumber;
    }

}
